package com.sc.nft.task;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.sc.nft.dao.UserCityServerCentreDao;
import com.sc.nft.dao.UserWorkRoomDao;
import com.sc.nft.dao.WorkCityServerAssessLogDao;
import com.sc.nft.entity.*;
import com.sc.nft.entity.dto.ApplicationRequirementGoUpAndDownDTO;
import com.sc.nft.entity.vo.UserWorkRoomExcludeWhiteListRankVO;
import com.sc.nft.enums.ApplicationRequirementUpOrDownEnum;
import com.sc.nft.service.UserCityServerCentreDownService;
import com.sc.nft.service.UserCityServerCentreService;
import com.sc.nft.service.UserWorkRoomDownService;
import com.sc.nft.service.UserWorkRoomService;
import com.tencentcloudapi.mgobe.v20201014.models.Room;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;
@Component
@RequiredArgsConstructor
@Slf4j
public class ApplicationRequirementTask {

    private final UserWorkRoomService userWorkRoomService;

    private final UserCityServerCentreService userCityServerCentreService;

    private final UserCityServerCentreDownService userCityServerCentreDownService;

    private final UserWorkRoomDownService userWorkRoomDownService;


    /**
     * 社区网点和城市服务中心的升降级
     * 升降级顺序: 先降级在升级
     * <p>
     * 降级顺序:  社区网点-城市服务中心
     * 升级顺序:  社区网点-城市服务中心
     */
    @XxlJob("userUpAndDown")
    public void equityPromotionAndDowngrade() {
        // 社区网点考核记录log对象
        WorkCityServerAssessLog workCityServerAssessLog = saveInitUpOrDownLog(ApplicationRequirementUpOrDownEnum.USER_WORK_ROOM);
        // 城市服务中心考核记录log对象
        WorkCityServerAssessLog urbanServiceCenterAssessLog = saveInitUpOrDownLog(ApplicationRequirementUpOrDownEnum.CITY);

        // 社区网点降级
        communityNetworkDowngrade(workCityServerAssessLog);

        // 城市服务中心降级
        cityServiceCenterDowngrade(urbanServiceCenterAssessLog);

        // 社区网点升级
        communityNetworkUpgrade(workCityServerAssessLog);

        // 城市服务中心升级
        cityServiceCenterUpgrade(urbanServiceCenterAssessLog);

        // 社区网点统计
        workCityServerAssessLog.setAssessAfter(userWorkRoomService.getCount());
        workCityServerAssessLog.updateById();

        // 城市服务中心统计
        urbanServiceCenterAssessLog.setAssessAfter(userCityServerCentreService.getCount());
        urbanServiceCenterAssessLog.updateById();
    }

    /**
     * 社区网点和城市服务中心的预升降级
     */
    @XxlJob("userPreUpAndDown")
    public void equityPrePromotionAndDowngrade() {
        // 社区网点预降级
        communityNetworkPreDowngrade();
        // 城市服务中心预降级
        cityServiceCenterPreDowngrade();
    }

    /**
     * 城市服务中心升级
     *
     * @param urbanServiceCenterAssessLog
     */
    private void cityServiceCenterUpgrade(WorkCityServerAssessLog urbanServiceCenterAssessLog) {
        // 城市服务中心升级使用被降级过的用户数据
        List<UserWorkRoomExcludeWhiteListRankVO> userCityServerCentreDowns = userCityServerCentreDownService.communityNetworkUpgrade();
        userCityServerCentreDowns.forEach(userCityServerCentreDown -> {
            // 城市服务中心升级
            userCityServerCentreService.upgrade(userCityServerCentreDown, urbanServiceCenterAssessLog.getId());
        });

    }
    /**
     * 社区网点升级
     *
     * @param workCityServerAssessLog
     */
    private void communityNetworkUpgrade(WorkCityServerAssessLog workCityServerAssessLog) {
        // 社区网点升级使用被降级过的用户数据
        List<UserWorkRoomExcludeWhiteListRankVO> userWorkRoomDowns = userWorkRoomDownService.communityNetworkUpgrade();
        userWorkRoomDowns.forEach(userWorkRoomDown -> {
            // 社区网点升级
            userWorkRoomService.upgrade(userWorkRoomDown, workCityServerAssessLog.getId());
        });
    }

    /**
     * 查询城市服务中心待降级数据
     */
    private List<UserWorkRoomExcludeWhiteListRankVO>  findCityServiceCenterDowngrade() {
        return userCityServerCentreService.getUserCityServerCentreExcludeWhiteListRank();
    }

    /**
     * 城市服务中心降级
     * @param urbanServiceCenterAssessLog
     */
    private void cityServiceCenterDowngrade(WorkCityServerAssessLog urbanServiceCenterAssessLog) {
        List<UserWorkRoomExcludeWhiteListRankVO> userCityServerCentreExcludeWhiteListRank = this.findCityServiceCenterDowngrade();
        // 设置考核前数量
        urbanServiceCenterAssessLog.setAssessBefore(userCityServerCentreService.getCount());
        // 考核城市服务中心降级
        userCityServerCentreExcludeWhiteListRank.forEach(data -> {
            UserCityServerCentre userCityServerCentre = userCityServerCentreService.selectByUserId(data.getUserId());
            // 城市服务中心降级
            userCityServerCentreService.downgrade(userCityServerCentre, urbanServiceCenterAssessLog.getId());
        });
    }

    /**
     * 城市服务中心预降级
     */
    private void cityServiceCenterPreDowngrade() {
        List<UserWorkRoomExcludeWhiteListRankVO> userCityServerCentreExcludeWhiteListRank = this.findCityServiceCenterDowngrade();
        // 考核城市服务中心降级
        userCityServerCentreExcludeWhiteListRank.forEach(data -> {
            UserCityServerCentre userCityServerCentre = userCityServerCentreService.selectByUserId(data.getUserId());
            // 城市服务中心降级
            userCityServerCentreService.preDowngrade(userCityServerCentre);
        });
    }

    /**
     * 查询待降级的社区网点
     */
    private List<UserWorkRoomExcludeWhiteListRankVO> findCommunityNetworkDowngrade() {
        // 获取社区网点的数据,让后挨个查询是否满足降级条件
        return userWorkRoomService.getUserWorkRoomExcludeWhiteListRank();
    }

    /**
     * 社区网点降级
     *
     * @param workCityServerAssessLog
     */
    private void communityNetworkDowngrade(WorkCityServerAssessLog workCityServerAssessLog) {
        // 获取社区网点的数据,让后挨个查询是否满足降级条件
        List<UserWorkRoomExcludeWhiteListRankVO> userWorkRoomExcludeWhiteListRank = this.findCommunityNetworkDowngrade();
        // 设置考核前数量
        workCityServerAssessLog.setAssessBefore(userWorkRoomService.getCount());
        // 考核社区网点降级.
        userWorkRoomExcludeWhiteListRank.forEach(data -> {
            // 社区网点降级
            userWorkRoomService.downgrade(userWorkRoomService.getByUserId(data.getUserId()), workCityServerAssessLog.getId());
        });
    }

    /**
     * 社区网点预降级
     *
     */
    private void communityNetworkPreDowngrade() {
        // 获取社区网点的数据,让后挨个查询是否满足降级条件
        List<UserWorkRoomExcludeWhiteListRankVO> userWorkRoomExcludeWhiteListRank = this.findCommunityNetworkDowngrade();
        // 考核社区网点预降级
        userWorkRoomExcludeWhiteListRank.forEach(data -> {
            // 社区网点预降级
            userWorkRoomService.preDowngrade(userWorkRoomService.getByUserId(data.getUserId()));
        });
    }

    /**
     * 新增初始化升级或降级Log数据
     *
     * @param type
     */
    private WorkCityServerAssessLog saveInitUpOrDownLog(ApplicationRequirementUpOrDownEnum type) {
        WorkCityServerAssessLog workCityServerAssessLog = new WorkCityServerAssessLog();
        workCityServerAssessLog.setType(type);
        workCityServerAssessLog.setAssessBefore(0);
        workCityServerAssessLog.setAssessAfter(0);
        workCityServerAssessLog.setAssessTime(DateUtil.date());
        workCityServerAssessLog.insert();
        return workCityServerAssessLog;
    }

}
